Linux 命令大全

Linux 命令大全
周言志Linux 命令大全
本文按场景分类,汇总常用的 Linux 命令与典型示例,适合作为日常运维与开发参考。命令示例以 bash 为主,适用于大多数 Linux 发行版(Ubuntu、CentOS、Debian 等)。
一、基础与系统信息
查看当前用户、主机、操作系统:
1
2
3whoami # 当前用户
hostnamectl # 主机与系统信息
uname -a # 内核与架构信息查看系统负载、运行时间:
1
2
3uptime
top # 交互式进程查看
htop # 更友好(需安装)查看内存与交换:
1
2free -h
vmstat 1 5查看磁盘使用情况:
1
2df -h # 分区磁盘使用
du -sh /path # 某目录大小
二、用户与权限
添加、删除用户与切换:
1
2
3sudo adduser username
sudo userdel -r username
su - username修改权限与属主:
1
2
3chmod 644 file.txt
chmod -R 755 /var/www
chown user:group filesudo 权限测试:
1
sudo -l
三、文件与目录操作
常用文件操作:
1
2
3
4
5ls -la
cat file
less file
head -n 50 file
tail -f /var/log/syslog复制、移动、删除:
1
2
3cp -r src/ dest/
mv oldname newname
rm -rf path/查找文件与内容:
1
2
3
4find / -name "*.log"
locate filename # 需 updatedb
grep -R --exclude-dir=node_modules "关键词" .
grep -n "pattern" file归档与压缩:
1
2
3
4tar czvf archive.tar.gz folder/
tar xzvf archive.tar.gz
zip -r archive.zip folder/
unzip archive.zip
四、进程与服务管理
列出进程与查杀:
1
2
3ps aux | grep proc_name
pkill -f process_keyword
kill -9 PIDsystemd 服务管理:
1
2
3
4
5
6sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl status nginx
sudo systemctl enable nginx # 开机自启
sudo systemctl disable nginx查看端口占用:
1
2
3ss -tulpn | grep :80
netstat -tulpn # 需安装 net-tools
lsof -i :3306
五、网络与远程操作
网络调试:
1
2
3
4ping -c 4 google.com
traceroute google.com # traceroute 或 tracepath
curl -I https://example.com
wget https://example.com/fileSSH 与文件传输:
1
2
3ssh user@host
scp file user@host:/path/
rsync -avz ./local/ user@host:/remote/ # 增量同步配置和查看网络接口:
1
2
3ip addr show
ip route show
nmcli device status # NetworkManager 环境
六、包管理(常见发行版)
Debian/Ubuntu (apt):
1
2
3sudo apt update
sudo apt install -y package
sudo apt remove packageCentOS/RHEL (yum/dnf):
1
2sudo yum install package
sudo dnf install package通用(源码/二进制):
1
./configure && make && sudo make install
七、日志与排错
查看日志(系统/服务):
1
2sudo journalctl -u nginx -f
tail -n 200 /var/log/syslog常用诊断工具:
1
2
3strace -p PID # 跟踪系统调用
ltrace -p PID # 跟踪库调用
debugfs / debug tools for filesystem issues权限与 SELinux:
1
2
3getenforce
setenforce 0 # 临时关闭 SELinux(谨慎)
ausearch -m avc -ts today # SELinux 审计查询
八、文本处理与管道
文本处理工具:
1
2
3
4awk '{print $1,$3}' file
sed -n '1,20p' file
cut -d',' -f1 file
sort file | uniq -c | sort -nr管道与重定向:
1
2command | grep pattern > out.txt
command 2>&1 | tee log.txt
九、Shell 脚本与自动化
简单脚本模板:
1
2
3
4
5
6
7
8
set -euo pipefail
IFS=$'\n\t'
# 示例:备份目录
src="/var/www"
dst="/backup/$(date +%F).tar.gz"
tar czf "$dst" "$src"定时任务(crontab):
1
2
3crontab -e
# 每天 2 点备份
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
十、实用技巧与安全建议
- 经常备份 /etc、数据库与重要数据。
- 生产环境慎用 root,优先使用 sudo。
- 打开防火墙并仅放行必要端口(ufw/iptables/firewalld)。
- 使用密钥认证替代密码登录 SSH,禁用 root 远程登录。
- 定期更新系统与第三方软件,关注安全公告。
参考与延伸阅读
- man command(例如 man ssh)
- tldr pages(简明命令示例)
- 官方发行版文档(Ubuntu、CentOS、Debian)
本文为常用命令速查手册,覆盖面广但不详尽。建议根据实际发行版与应用场景查阅 man 页与官方文档以获取详细选项与用法。
评论
匿名评论隐私政策
TwikooWaline
✅ 你无需删除空行,直接评论以获取最佳展示效果






